﻿package com.tudou.home.common.button
{
    import com.tudou.home.common.events.*;
    import com.tudou.skin.common.button.*;
    import com.tudou.skin.common.events.*;
    import flash.display.*;

    public class PrevNextButton extends Sprite implements IPrevNext
    {
        protected var index:uint = 0;
        protected var next:BaseButton;
        protected var length:uint = 0;
        protected var previous:BaseButton;

        public function PrevNextButton(param1:InteractiveObject, param2:InteractiveObject, param3:InteractiveObject, param4:InteractiveObject)
        {
            previous = new BaseButton();
            previous.addStateButton(param1);
            previous.addStateButton(param2);
            addChild(previous);
            next = new BaseButton();
            next.addStateButton(param3);
            next.addStateButton(param4);
            next.x = previous.width;
            addChild(next);
            previous.activeIndex = 1;
            next.activeIndex = 1;
            initListeners();
            return;
        }// end function

        protected function clickHlr(event:ButtonEvent) : void
        {
            if (event.clickIndex == 1)
            {
                return;
            }
            switch(event.target)
            {
                case previous:
                {
                    dispatchEvent(new PrevNextEvent(PrevNextEvent.PREVIOUS, index));
                    break;
                }
                case next:
                {
                    dispatchEvent(new PrevNextEvent(PrevNextEvent.NEXT, index));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function setLength(param1:uint) : void
        {
            length = param1;
            return;
        }// end function

        public function getLength() : uint
        {
            return length;
        }// end function

        protected function initListeners() : void
        {
            previous.addEventListener(ButtonEvent.CLICK, clickHlr);
            next.addEventListener(ButtonEvent.CLICK, clickHlr);
            return;
        }// end function

        public function setIndex(param1:int) : void
        {
            if (param1 >= length)
            {
                param1 = length;
            }
            if (param1 <= 0)
            {
                param1 = 0;
            }
            this.index = param1;
            previous.activeIndex = param1 > 0 ? (0) : (1);
            next.activeIndex = param1 < (length - 1) ? (0) : (1);
            return;
        }// end function

        public function getIndex() : int
        {
            return index;
        }// end function

    }
}
